package com.blog.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.blog.entity.Article;
import com.blog.vo.ArticleListVo;


public interface ArticleMapper {

	@Select("SELECT * FROM article WHERE id = #{id}")
	Article findById(@Param("id") Long id);

	@Delete("delete from article where id = #{id}")
	int deleteById(@Param("id") int id);

	/**
	 * 文章列表分页动态查询
	 * @param typeId
	 * @param title
	 * @param startIndex
	 * @param pageSize
	 * @return
	 */
	@Select("<script>" 
			+ "select a.id,a.create_time,a.title,a.modify_time,t.name type,pv.page_view pageView from article a "
			+ " inner join user u on u.id=a.user_id" 
			+ " inner join article_type t on t.id=a.type_id" 
			+ " left join article_page_view pv on pv.id=a.page_view_id" 
			+ " <where>"
			+ " <if test=\"typeId != null and typeId != '-1'\"> AND a.type_id=#{typeId}</if>" 	
			+ " <if test=\"title != null and title != ''\"> AND a.title like #{title}</if>" 
			+ " <if test=\"username != null and username != ''\"> AND u.username=#{username}</if>" 
			+ " </where>"
			+ " order by a.create_time desc limit #{startIndex},#{pageSize}" + "</script>")
	List<ArticleListVo> pageList(@Param("typeId") String typeId, 
			@Param("title") String title,
			@Param("startIndex") Integer startIndex,
			@Param("pageSize") Integer pageSize,
			@Param("username") String username
			);
	
	
	
	@Select("<script>" 
			+ "select count(a.id) from article a "
			+ " inner join user u on u.id=a.user_id" 
			+ " inner join article_type t on t.id=a.type_id" 
			+ " left join article_page_view pv on pv.id=a.page_view_id" 
			+ " <where>"
			+ " <if test=\"typeId != null and typeId != '-1'\"> AND a.type_id=#{typeId}</if>" 	
			+ " <if test=\"title != null and title != ''\"> AND a.title like #{title}</if>" 
			+ " <if test=\"username != null and username != ''\"> AND u.username=#{username}</if>" 
			+ " </where> </script>")
	Long totalCount(@Param("typeId") String typeId, 
			@Param("title") String title,
			@Param("username") String username
			);
	/*
	 * @Update("update article set username = #{username} where id = #{id}")
	 * public int update(@Param("username") String username, @Param("id") String
	 * id);
	 * 
	 * @Insert("insert into article(username) values(#{username})") public int
	 * save(@Param("username") String username);
	 */

	@Insert("insert into article(id,title,info,text,user_id,type_id,page_view_id) values(#{id},#{title},#{info},#{text},#{userId},#{typeId},#{PageViewId})")
	public int save(@Param("id") String id
			,@Param("title") String title
			,@Param("info") String info
			,@Param("text") String text
			,@Param("userId") String userId
			,@Param("typeId") String typeId
			,@Param("PageViewId") String PageViewId);
}
